<?php

namespace Game\Logic;
use \Common\Logic\BaseLogic;
/**
 * 问题logic
 */
class QueLogic extends BaseLogic
{

    private $queModel;

    public function __construct()
    {
        parent::__construct();
        $this->queModel = D('Question');
    }

    /**
     * 问题统计
     * @param array $where
     * @return mixed
     */
    public function statistics($where = array())
    {
        return $this->queModel->where($where)->count();
    }

    /**
     * 获取列表
     * @param int $page
     * @param int $size
     * @param $where
     * @return mixed
     */
    public function getList($page = 1, $size = 20, $where)
    {
        $list = $this->queModel->where($where)->page($page, $size)->order("id desc")->select();
        $total = $this->statistics($where);
        return static::$res->suc("成功", array(
            "list" => $list,
            "total" => $total
        ));
    }

    /**
     * 取一条数据
     * @param type $where
     * @return type
     */
    public function getInfo($where)
    {
        $info = $this->queModel->where($where)->find();
        return static::$res->suc("成功", $info);
    }


    /**
     * 添加问题
     * @param $userId
     * @param $info
     * @return mixed
     */
    public function add($userId, $info)
    {
        if (empty($userId)) {
            return static::$res->err("添加人不能为空");
        }
        static $fields = array(
            "title" => array("empty" => "标题不能为空"),
            "ask" => array("empty" => "回答不能为空"),
            "order_id" => array()
        );

        $data = array();
        foreach ($fields as $title => $vo) {
            $val = $info[$title];
            if (!empty($vo['empty'])) {
                if (empty($val)) {
                    return static::$res->err($vo['empty']);
                }
            }
            $data[$title] = $val;
        }

        $data['state'] = 1;
        $data['add_time'] = date('Y-m-d H:i:s');
        if (empty($data['order_id'])) {
            $data['order_id'] = 1;
        }

        $id = $this->queModel->add($data);
        if (empty($id)) {
            return static::$res->err("数据库异常，添加失败");
        }

        //添加成功，返回用户信息
        return static::$res->suc("添加成功");
    }

    /**
     * 编辑用户
     * @param $id
     * @param string $editId
     * @param array $param
     * @return mixed
     */
    public function update($id, $editId, $param = array())
    {
        if (empty($id)) {
            return static::$res->err("id不能为空");
        }

        if (empty($editId)) {
            return static::$res->err("编辑人不能为空");
        }

        $where = array(
            "id" => $id,
            "state" => 1
        );

        unset($param['id']);
        unset($param['add_time']);

        $userInfoRes = $this->getInfo($where);
        $userInfo = $userInfoRes['Result'];
        if (empty($userInfo)) {
            return static::$res->err("未找到问题");
        }

        if (empty($param)) {
            return static::$res->err("没有任何改变");
        }

        $param['update_time'] = date('Y-m-d H:i:s');
        $param['update_user'] = $editId;

        $count = $this->queModel->where($where)->save($param);
        if (empty($count)) {
            return static::$res->err("数据库异常，没有任何改变");
        }

        return static::$res->suc("编辑成功");
    }
}
